Interacting with Scifeon through Python
This guide explains how to extract data from Scifeon via a Python script.
In all of the Python code examples below, remember to replace your-domain
with your own Scifeon cloud domain.
Prerequisites
In order to interact with an API in python the requests library needs to be installed.
This is done by typing in the console:
pip install --upgrade requests
If you are using conda to run your python use:
conda install --upgrade requests
Now you can design your python script.
Authorize
First a Personal Access Token (PAT) needs to be generated to get access:
- Open your user profile: go to https://your-domain.scifeon.cloud/#/user/profile
- Click "New PAT" and copy the PAT generated and shown in the dialog (starts with PAT).
- Paste the PAT into the
personal_access_token
field in the script below.Example: Getting an experiment
username = "username@company.com"
personal_access_token = ""
url = "https://your-domain.scifeon.cloud/api/entity/experiment/EX00001"
header = {
"Content-Type": "application/json"
}
response = requests.get(url, headers=header, auth=(username, personal_access_token))
experiment = response.json()
The output experiment
will be a dictionary, which you can further manipulate in pandas or your favorite dataframe editor.
If you want to print the data in a JSON comparable format you can use the following function:
def jprint(obj):
# create a formatted string of the Python JSON object
text = json.dumps(obj, sort_keys=True, indent=4)
print(text)
jprint(experiment)
Example: Query all results related to a fermentation
You can also request data using dataset-query. An example of this could be requesting all samples related to the fermentation named GDF1234
:
username = "username@company.com"
personal_access_token = ""
url = "https://your-domain.scifeon.cloud/api/query/dataset"
header = {
"Content-Type": "application/json"
}
query = [
{
"eClass": "Fermentation",
"entity": "fermentation",
"filters": [{ "field": "name", "value": "GDF1234"}],
},
{
"eClass": "Sample",
"collection": "samples",
"filters": [{ "field": "fermentationID", "value": "$fermentation.id"}],
},
{
"eClass": "ResultValue",
"collection": "resultValues",
"filters": [{ "field": "SubjectID", "in": "samples.ID" }],
}]
response = requests.request("POST", url, headers=header, json=query, auth=(username, personal_access_token))
queryResult = response.json()
The format of the queryResult
can be found in datasetQuery documentation here
The queryResult
will again be a dictionary, which can be manipulated into a dataframe through pandas.
Jupyter
There has been created a Jupyter notebook, with a tutorial for importing from scifeon. It can be downloaded here Download Jupyter notebook
Next steps
The HTTP API documentation lists all possibilities for querying and saving data.